import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class ColorBox extends JPanel implements MouseListener{

	public Canvas parent;
	private int status = 0;
	public JPanel colorPanel, colorPreview;
	private JLabel[][] colorTile;
	private Color[][] color;
	
	public ColorBox() {
		setLayout(new BorderLayout());
		
		colorPanel = new JPanel();
		colorPanel.setLayout(new GridLayout(9,3));
		colorPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		
		colorTile = new JLabel[9][3];
		color = new Color[9][3];
		
		for(int j = 0;j<9;j++) {
            for(int i = 0;i<3;i++) {
				color[j][i] = new Color((j%3)*127,(j/3)*127,i*127);
				colorTile[j][i] = new JLabel();
				colorTile[j][i].setPreferredSize(new Dimension(14,14));
				colorTile[j][i].setOpaque(true);
                colorTile[j][i].setBackground(color[j][i]);
				colorTile[j][i].setBorder(BorderFactory.createLineBorder(Color.WHITE));
				colorTile[j][i].addMouseListener(this);
				colorPanel.add(colorTile[j][i]);
            }
		}
		
		colorPreview = new JPanel();
		colorPreview.setBackground(new Color(51,51,51));
		colorPreview.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		colorPreview.addMouseListener(this);
		
		add(colorPanel, BorderLayout.NORTH);
		add(colorPreview, BorderLayout.CENTER);
	}
	
	public void mouseClicked(MouseEvent e){
		for(int j = 0;j<9;j++) {
            for(int i = 0;i<3;i++) {
				if(e.getSource() == colorTile[j][i]) {
					colorPreview.setBackground(color[j][i]);
					parent.setForeGroundColor(color[j][i]);
					return;
				}
			}
		}
	}
	public void mouseEntered(MouseEvent e) {
		for(int j = 0;j<9;j++) {
            for(int i = 0;i<3;i++) {
				if(e.getSource() == colorTile[j][i])
					colorTile[j][i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
			}
		}
	}
	public void mouseExited(MouseEvent e) {
		for(int j = 0;j<9;j++) {
            for(int i = 0;i<3;i++) {
				if(e.getSource() == colorTile[j][i])
					colorTile[j][i].setBorder(BorderFactory.createLineBorder(Color.WHITE));
			}
		}
	}
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
}